﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace shell
{
    public class SerialHelper
    {
        private SerialPort serialPort;//串口对象
        public SerialHelper(string portName = "COM1", int baudRate = 115200)
        {
            serialPort = new SerialPort();
            serialPort.PortName = portName;
            serialPort.BaudRate = baudRate;
            serialPort.DataReceived += serialPort_DataReceived;
        }


        void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                int readlen = serialPort.BytesToRead;
                if (readlen > 0)
                {
                    byte[] readBuffer = new byte[readlen];
                    int count = serialPort.Read(readBuffer, 0, readlen);
                    if (count > 0)
                    {
                        //接收到的数据推送到接收工厂
                        for (int i = 0; i < count; i++)
                        {
                            RecParser(readBuffer[i]);
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ~SerialHelper()
        {
            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }
            serialPort.Dispose();
        }

        public void Start()
        {
            try
            {
                serialPort.Open();
            }
            catch (Exception)
            {
                Console.WriteLine("无法打开串口");
            }

        }

        public void Stop()
        {
            serialPort.Close();
        }

        public void Write(char data)
        {
            char[] send = new char[1];
            send[0] = data;
            serialPort.Write(send, 0, 1);
        }

        public enum RecState
        {
            normal,
            escap
        }
        private RecState recState = RecState.normal;
        private List<byte> chararray = new List<byte>();
        private void RecParser(byte data)
        {
            switch (recState)
            {
                case RecState.normal:   //正常字符接收
                    if (data == 0x1b)
                    {
                        recState = RecState.escap;
                        break;
                    }
                    Console.Write((char)data);
                    break;
                case RecState.escap:
                    chararray.Add(data);
                    if (((char)data >= 'A' && (char)data <= 'Z') || ((char)data >= 'a' && (char)data <= 'z'))
                    {
                        ANSI.ParserANSI(System.Text.Encoding.Default.GetString(chararray.ToArray()));
                        chararray.Clear();
                        recState = RecState.normal;
                    }
                    break;
            }
        }
    }
}
